﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Albums;

    public class AlbumCreatedEventArgs : EventArgs
    {
        Guid albumId;
        public Guid AlbumID { get { return albumId; } }

        public AlbumCreatedEventArgs(Guid aid)
        {
            albumId = aid;
        }
    }

    public partial class CreateAlbum : SharedUserControl
    {
        string urlOnCreateComplete = string.Empty;
        string newAlbumIdQueryStringField = "aid";
        public event EventHandler<AlbumCreatedEventArgs> AlbumCreated;

        public string UrlOnCreateComplete 
        {
            get { return urlOnCreateComplete; }
            set { urlOnCreateComplete = value; }
        }

        public string NewAlbumIdQueryStringField { get { return newAlbumIdQueryStringField; }
            set { newAlbumIdQueryStringField = value; }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            FormView1.ItemInserting += new FormViewInsertEventHandler(FormView1_ItemInserting);
            LinqDataSource1.Inserted += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Inserted);
        }

        void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            try
            {
                Sc_SharedAlbum result = (Sc_SharedAlbum)e.Result;
                AlbumCreated(this, new AlbumCreatedEventArgs(result.AlbumId));
                if (urlOnCreateComplete != string.Empty)
                {
                    string j = urlOnCreateComplete.Contains("?") ? "&" : "?";
                    Response.Redirect(string.Format("{0}{3}{1}={2}", urlOnCreateComplete, newAlbumIdQueryStringField, result.AlbumId, j));
                }
            }
            catch
            {
            }
        }


        void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["ContextType"] = EntityType;
            e.Values["ContextId"] = ContextId;
            e.Values["CreateDate"] = DateTime.Now;
            e.Values["LastUpdate"] = DateTime.Now;
            e.Values["PhotoCount"] = 0;
            e.Values["AlbumIndex"] = 1;
            PrivacyDropDownList d = (PrivacyDropDownList)FormView1.FindControl("PrivacyDropDownList1");
            e.Values["Privacy"] = d.NamedPrivacy;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            panel1.CssClass = CssClass;
        }
    }
}